﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class sellCourse : System.Web.UI.Page
{
    db_users user = new db_users();
    db_course course = new db_course();
    public string cGold = "";
    public string uGold = "";
    public string courseName = "";

    public bool isLogin = false; //是否登录
    public bool isSell = false; //是否购买

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] == null)
        {
            Response.Redirect("index.aspx");
        }
        DataSet ds = new DataSet();
        string courseID = Request.QueryString["id"];
        ds = course.selectCourseInfo_id(courseID);
        if (ds.Tables[0].Rows.Count == 0 || "1".Equals(ds.Tables[0].Rows[0]["status"].ToString()))
        {
            Response.Write("<script language=javascript>alert('找不到数据！');</script>");
            return;
        }
        else
        {
            string kind = ds.Tables[0].Rows[0]["coursetype"].ToString();
            courseName = ds.Tables[0].Rows[0]["courseName"].ToString();
            this.Page.Title = courseName + "_" + kind + "_下载";
            
        }

        string stuid = "";
        //判断是否登录
        try
        {
            if (Session["stuId"] != null)
            {
                isLogin = true;
                stuid = Session["stuId"].ToString();
            }
            else
            {
                isLogin = false;
            }
        }
        catch
        {
            isLogin = false;
        }

        //是否登录
        if (isLogin)
        {
            isSell = course.CourseisSell(stuid, courseID);

            //如果未购买就显示相关信息
            if (!isSell)
            {
                cGold = ds.Tables[0].Rows[0]["gold"].ToString();
                uGold = user.selectUserONE_id(stuid).Tables[0].Rows[0]["gold"].ToString();
            }
        }
        else
        {
            Response.Write("<script language=javascript>alert('登录超时，或没有登录，请重新登录！');</script>");
            Response.End();
        }
    }
    protected void ibtn_sell_Click(object sender, ImageClickEventArgs e)
    {
        string stuid = "";
        string courseID = Request.QueryString["id"];

        //是否已经登录
        try
        {
            if (Session["stuId"] != null)
            {
                stuid = Session["stuId"].ToString();
            }
            else
            {
                Response.Write("<script language=javascript>alert('登录超时，或没有登录，请重新登录！');top.location.href='../stu/login.aspx';</script>");
            }

        }
        catch
        {
            Response.Write("<script language=javascript>alert('登录超时，或没有登录，请重新登录！');top.location.href='../stu/login.aspx';</script>");
        }

        //检查是否购买
        if (course.CourseisSell(stuid, courseID))
        {
            Response.Write("<script language=javascript>alert('该课程已经购买');window.opener.location.reload();window.opener=null;window.close();</script>");
            Response.End();
        }
        else
        {
            float fCGold = float.Parse(course.selectCourseInfo_id(courseID).Tables[0].Rows[0]["gold"].ToString());
            float fUGold = float.Parse(user.selectUserONE_id(stuid).Tables[0].Rows[0]["gold"].ToString());
            if (fCGold < fUGold)
            {
                //购买操作
                if (course.sellCourse(stuid, courseID, fCGold.ToString()))
                {
                    Response.Write("<script language=javascript>alert('购买成功，本次操作扣取金币值：" + fCGold + "');</script>");
                }
            }
            else
            {
                Response.Write("<script language=javascript>alert('用户剩余金币值不足');</script>");
                Response.End();
            }
        }
    }
    protected void ibtn_close_Click(object sender, ImageClickEventArgs e)
    {
        this.Response.Write("<script>window.opener.location.reload();window.opener=null;window.close();</script>");
    }
}